package org.jeecg.modules.cw.service.impl;

import org.jeecg.modules.cw.entity.CwZzrygzmxbTj;
import org.jeecg.modules.cw.entity.CwZzrygzmxb;
import org.jeecg.modules.cw.mapper.CwZzrygzmxbMapper;
import org.jeecg.modules.cw.mapper.CwZzrygzmxbTjMapper;
import org.jeecg.modules.cw.service.ICwZzrygzmxbTjService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 在职人员工资明细表
 * @Author: jeecg-boot
 * @Date:   2025-09-09
 * @Version: V1.0
 */
@Service
public class CwZzrygzmxbTjServiceImpl extends ServiceImpl<CwZzrygzmxbTjMapper, CwZzrygzmxbTj> implements ICwZzrygzmxbTjService {

	@Autowired
	private CwZzrygzmxbTjMapper cwZzrygzmxbTjMapper;
	@Autowired
	private CwZzrygzmxbMapper cwZzrygzmxbMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CwZzrygzmxbTj cwZzrygzmxbTj, List<CwZzrygzmxb> cwZzrygzmxbList) {
		cwZzrygzmxbTjMapper.insert(cwZzrygzmxbTj);
		if(cwZzrygzmxbList!=null && cwZzrygzmxbList.size()>0) {
			for(CwZzrygzmxb entity:cwZzrygzmxbList) {
				//外键设置
				entity.setMianId(cwZzrygzmxbTj.getId());
				cwZzrygzmxbMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CwZzrygzmxbTj cwZzrygzmxbTj,List<CwZzrygzmxb> cwZzrygzmxbList) {
		cwZzrygzmxbTjMapper.updateById(cwZzrygzmxbTj);
		
		//1.先删除子表数据
		cwZzrygzmxbMapper.deleteByMainId(cwZzrygzmxbTj.getId());
		
		//2.子表数据重新插入
		if(cwZzrygzmxbList!=null && cwZzrygzmxbList.size()>0) {
			for(CwZzrygzmxb entity:cwZzrygzmxbList) {
				//外键设置
				entity.setMianId(cwZzrygzmxbTj.getId());
				cwZzrygzmxbMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		cwZzrygzmxbMapper.deleteByMainId(id);
		cwZzrygzmxbTjMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cwZzrygzmxbMapper.deleteByMainId(id.toString());
			cwZzrygzmxbTjMapper.deleteById(id);
		}
	}
	
}
